package com.xinpujing.caiji.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;

public class CommonService {

    private JdbcTemplate jdbcTemplate;

    /**
     * 查询是否存在 ,  type, number, data
     * @param type
     * @param number
     * @param data
     */
    public Boolean exist(int type , String number , String data){
        return jdbcTemplate.query("select * from jcb_lottery_data where type=? and number=? and data=?", new Object[]{type, number, data}, new ResultSetExtractor<Boolean>() {
            @Override
            public Boolean extractData(ResultSet resultSet) throws SQLException, DataAccessException {
                if(resultSet.next()){
                    return Boolean.TRUE;
                }
                return Boolean.FALSE;
            }
        });

    }

    /**
     * 插入数据
     * @param type
     * @param time
     * @param number
     * @param data
     */
    public void insert(int type , String time , String number , String data){
        jdbcTemplate.update("insert into jcb_lottery_data(type, time, number, data) values(?,?,?,?)" , type , time , number , data);
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
